package org.codeart.tank.model;

import org.codeart.Dir;
import org.codeart.TankFrame;

import java.awt.*;

/**
 * 子弹类
 */
public class Bullet {

    /**
     * 子弹初始速度为 10 像素
     */
    private static final int SPEED = 10;

    /**
     * 子弹宽度
     */
    private static final int WIDTH = 10;

    /**
     * 子弹高度
     */
    private static final int HEIGHT = 10;
    
    private int x;
    
    private int y;
    
    private Dir dir;
    
    private TankFrame tankFrame;
    
    private boolean alive = true;

    public Bullet(int x, int y, Dir dir, TankFrame tankFrame) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.tankFrame = tankFrame;
    }

    public void paint(Graphics g) {
        if (!alive) {
            tankFrame.bullets.remove(this);
            return;
        }
        Color color = g.getColor();
        g.setColor(Color.RED);
        g.fillOval(x, y, WIDTH, HEIGHT);
        g.setColor(color);
        move();
    }

    private void move() {
        switch (dir) {
            case LEFT:
                x -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
        }
        
        if (x <= 0 || y <= 0 ||x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) {
            alive = false;
        }
    }
}
